package zhizelian;

// 采购请求类
class PurchaseRequest {
    private String id;
    private double amount;
    private String purpose;

    public PurchaseRequest(String id, double amount, String purpose) {
        this.id = id;
        this.amount = amount;
        this.purpose = purpose;
    }

    public String getId() {
        return id;
    }

    public double getAmount() {
        return amount;
    }

    public String getPurpose() {
        return purpose;
    }
}

// 抽象审批者类
abstract class Approver {
    private String name;
    private Approver successor;

    public Approver(String name) {
        this.name = name;
    }

    public void setSuccessor(Approver successor) {
        this.successor = successor;
    }

    public final void processRequest(PurchaseRequest request) {
        if (approveRequest(request)) {
            System.out.println(name + " 审批通过采购单：" + request.getId());
        } else if (successor != null) {
            System.out.println(name + " 无法审批，转交给 " + successor.getClass().getSimpleName());
            successor.processRequest(request);
        } else {
            System.out.println("采购单 " + request.getId() + " 无法被处理，没有合适的审批者");
        }
    }

    protected abstract boolean approveRequest(PurchaseRequest request);

    public Approver getSuccessor() {
        return null;
    }
}

// 主任类
class Director extends Approver {
    public Director(String name) {
        super(name);
    }

    @Override
    protected boolean approveRequest(PurchaseRequest request) {
        if (request.getAmount() <= 10000) {
            System.out.println("主任 " + getName() + " 审批了采购单：" + request.getId() + "，金额：" + request.getAmount());
            return true;
        }
        return false;
    }

    private String getName() {
        // 这里简化处理，实际应通过父类方法获取
        return "Director";
    }
}

// 部门经理类
class Manager extends Approver {
    public Manager(String name) {
        super(name);
    }

    @Override
    protected boolean approveRequest(PurchaseRequest request) {
        if (request.getAmount() <= 50000) {
            System.out.println("部门经理 " + getName() + " 审批了采购单：" + request.getId() + "，金额：" + request.getAmount());
            return true;
        }
        return false;
    }

    private String getName() {
        // 这里简化处理，实际应通过父类方法获取
        return "Manager";
    }
}

// 副总经理类
class DeputyGeneralManager extends Approver {
    public DeputyGeneralManager(String name) {
        super(name);
    }

    @Override
    protected boolean approveRequest(PurchaseRequest request) {
        if (request.getAmount() <= 100000) {
            System.out.println("副总经理 " + getName() + " 审批了采购单：" + request.getId() + "，金额：" + request.getAmount());
            return true;
        }
        return false;
    }

    private String getName() {
        // 这里简化处理，实际应通过父类方法获取
        return "DeputyGM";
    }
}

// 总经理类
class GeneralManager extends Approver {
    public GeneralManager(String name) {
        super(name);
    }

    @Override
    protected boolean approveRequest(PurchaseRequest request) {
        if (request.getAmount() <= 200000) {
            System.out.println("总经理 " + getName() + " 审批了采购单：" + request.getId() + "，金额：" + request.getAmount());
            return true;
        }
        return false;
    }

    private String getName() {
        // 这里简化处理，实际应通过父类方法获取
        return "GeneralManager";
    }
}

// 会议类
class Meeting {
    public void processRequest(PurchaseRequest request) {
        System.out.println("召开会议审批采购单：" + request.getId() + "，金额：" + request.getAmount());
        System.out.println("会议审批通过采购单：" + request.getId());
    }
}

// 审批链构建类
class ApprovalChainBuilder {
    public static Approver buildChain() {
        Approver director = new Director("张三");
        Approver manager = new Manager("李四");
        Approver deputyGM = new DeputyGeneralManager("王五");
        Approver generalManager = new GeneralManager("赵六");
        
        director.setSuccessor(manager);
        manager.setSuccessor(deputyGM);
        deputyGM.setSuccessor(generalManager);
        
        return director;
    }
}